package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.pojo.Course;
import com.woniu.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("course")
public class CourseController {
    @Autowired
    CourseService courseService;

    /*增加跳转*/
    @RequestMapping("addIndex")
    public String addIndex(){
        return "WEB-INF/course/add";
    }
    /*增*/
    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(Course course){
        Map<String,Object> map = new HashMap<>();
        try {
           courseService.add(course);
           map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /*删除*/
    @RequestMapping("deleteById")
    @ResponseBody
    public Map<String,Object> deleteById(Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
        courseService.deleteById(id);
        map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
   /* 批量删除*/
    @RequestMapping("deleteByIds")
    @ResponseBody
    public Map<String,Object> deleteByIds(Integer[] ids){
        Map<String,Object> map = new HashMap<>();
        try {
            if(ids!=null){
                courseService.deleteByIds(ids);
                map.put("result",true);
            }else {
                map.put("result","empty");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
    /*编辑跳转*/
    @RequestMapping("edit")
    public String edit(Model model,Integer id){
        model.addAttribute("id",id);
        return "WEB-INF/course/edit";
    }
    /*查一条*/
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String,Object> findOne(Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
          Course course = courseService.findOne(id);
          map.put("course",course);
          map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
  /*  改*/
    @RequestMapping("updateById")
    @ResponseBody
    public Map<String,Object> updateById(Course course){
        Map<String,Object> map = new HashMap<>();
        try {
           courseService.updateById(course);
           map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }
   /* 展示，跳转*/
    @RequestMapping("index")
    public String index(){
        return "WEB-INF/course/index";
    }
    /*查，按条件*/
    @RequestMapping("selectByExample")
    @ResponseBody
    public Map<String,Object> selectByExample(Course course, @RequestParam(name = "pageNum",defaultValue = "1")
            Integer pageNum,@RequestParam(name = "pageSize",defaultValue = "3")
                                              Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Course> courseList = courseService.selectByExample(course);
            PageInfo<Course> pageInfo = new PageInfo<>(courseList);
            map.put("courseList",courseList);
            map.put("pageInfo",pageInfo);
            map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }


}
